package com.web.base.auth.provider;

import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.auth.cache.RoleMenuAuthCache;
import com.web.base.auth.cache.RoleUserCache;
import com.web.base.auth.exception.MenuAuthException;
import com.web.base.auth.facade.MenuAuthFacade;
import com.web.base.auth.message.MenuAuthRequest;
import com.web.base.auth.message.MenuAuthResponse;
import com.web.base.auth.model.MenuAuth;
import com.web.base.auth.service.MenuAuthService;
import com.web.base.auth.util.MenuAuthConvertor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("authMenuAuthProvider")
public class MenuAuthProvider implements MenuAuthFacade
{
	private static Logger logger = LoggerFactory.getLogger(MenuAuthProvider.class);
	
	@Autowired
	private MenuAuthService menuAuthService;
	@Autowired
	private RoleUserCache roleUserCache;
	@Autowired
	private RoleMenuAuthCache roleMenuAuthCache;
	
	public MenuAuthProvider()
	{
	}

	@Override
	public MenuAuthResponse add(MenuAuthRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		MenuAuth menuAuth = MenuAuthConvertor.toMenuAuth(request);
		menuAuth = menuAuthService.save(menuAuth);
		MenuAuthResponse response = MenuAuthConvertor.toResponse(menuAuth);
		return response;
	}

	@Override
	public MenuAuthResponse update(MenuAuthRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		MenuAuth menuAuth = MenuAuthConvertor.toMenuAuth(request);
		menuAuth = menuAuthService.save(menuAuth);
		MenuAuthResponse response = MenuAuthConvertor.toResponse(menuAuth);
		return response;
	}

	@Override
	public MenuAuthResponse get(Integer id)
	{
		if (id == null)
			throw new MenuAuthException("菜单权限ID不能为空");
			
		MenuAuth menuAuth = menuAuthService.get(id);
		MenuAuthResponse response = MenuAuthConvertor.toResponse(menuAuth);
		return response;
	}
	
	@Override
	public void delete(Integer id)
	{
		if (id == null)
			throw new MenuAuthException("菜单权限ID不能为空");
		
		MenuAuth menuAuth = new MenuAuth();
		menuAuth.setId(id);
		menuAuthService.delete(menuAuth);
	}

	@Override
	public List<MenuAuthResponse> list(MenuAuthRequest request)
	{
		MenuAuth menuAuth = MenuAuthConvertor.toMenuAuth(request);
		List<MenuAuth> list = menuAuthService.list(menuAuth);
		List<MenuAuthResponse> responseList = MenuAuthConvertor.toResponse(list);
		return responseList;
	}

	@Override
	public boolean auth(Integer userId, String url)
	{
		if (userId == null || url == null || url.isEmpty())
			throw new MenuAuthException("用户ID或URL不能为空");
			
		List<Integer> roleIdList = roleUserCache.listRoleId(userId);
		boolean auth = roleMenuAuthCache.auth(roleIdList, url);
		logger.info("[user_id={}] 是否有权限访问 [url={}]：{}", userId, url, auth);
		return auth;
	}
	
}
